package com.ezcoding.service;

import com.ezcoding.dao.*;
import com.ezcoding.model.*;
import com.ezcoding.utils.NeteaseMessageUtil;
import com.ezcoding.utils.PhoneFormatCheckUtil;
import com.ezcoding.utils.StatusUtil;
import com.ezcoding.utils.Tools;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserService {

    private static UserService instance = new UserService();

    public static UserService getInstance() {
        return instance;
    }

    public Map<String, Object> register(String username, String password, String inviteCode) {
        if (Tools.checkParaIsInValidate(username, password, inviteCode)) {
            return StatusUtil.getFailedStatusWithMessage("参数无效！");
        }

        User inviteUser = UserDao.dao.getUserByUsername(inviteCode);

        if (inviteUser == null) {
            return StatusUtil.getFailedStatusWithMessage("邀请码无效！");
        }

        if (!PhoneFormatCheckUtil.isPhoneLegal(username)) {
            return StatusUtil.getFailedStatusWithMessage("请输入正确的手机号作为用户名！");
        }

        User user = UserDao.dao.getUserByUsername(username);

        if (user != null) {
            return StatusUtil.getFailedStatusWithMessage("该用户已存在！");
        }

        user = new User();
        user.setUsername(username);
        user.setPassword(password);
        user.setOperatePassword(password);
        user.setIsPlatform(1);

        user.setUpUsername1(inviteUser.getUsername());
        user.setUpUsername2(inviteUser.getUpUsername1());
        user.setUpUsername3(inviteUser.getUpUsername2());
        user.setUpUsername4(inviteUser.getUpUsername3());
        user.setUpUsername5(inviteUser.getUpUsername4());

        return StatusUtil.getStatus(user.save(), "注册成功", "注册失败");
    }

    public Map<String, Object> sendMessage(String mobile) {
        if (Tools.checkParaIsInValidate(mobile)) {
            return StatusUtil.getFailedStatusWithMessage("参数无效！");
        }

        if (!PhoneFormatCheckUtil.isPhoneLegal(mobile)) {
            return StatusUtil.getFailedStatusWithMessage("手机号无效！");
        }

        String resCode = NeteaseMessageUtil.sendMessage(mobile);

        if (resCode == null) {
            return StatusUtil.getFailedStatusWithMessage("获取验证码失败！");
        }

        Map<String, Object> res = new HashMap<>();
        Map<String, Object> data = new HashMap<>();

        data.put("resCode", resCode);
        res.put("data", data);

        return StatusUtil.getSuccessStatusWithMessage(res, "获取验证码成功！");
    }

    public Map<String, Object> login(String username, String password) {
        if (Tools.checkParaIsInValidate(username, password)) {
            return StatusUtil.getFailedStatusWithMessage("参数无效！");
        }

        User user = UserDao.dao.getUserByUsername(username);

        //用户名不存在
        if (user == null) {
            return StatusUtil.getFailedStatusWithMessage("用户名不存在！");
        }

        if (!password.equals(user.getPassword())) {
            return StatusUtil.getFailedStatusWithMessage("密码错误！");
        }

        return StatusUtil.getSuccessStatusWithMessage("登录成功");
    }

    public Map<String, Object> changePassword(String username, String oldPassword, String newPassword, String passwordType) {
        if (Tools.checkParaIsInValidate(username, oldPassword, newPassword, passwordType)) {
            return StatusUtil.getFailedStatusWithMessage("参数无效！");
        }

        int type;
        if (passwordType.equals("0")) {
            type = 0;
        } else if (passwordType.equals("1")) {
            type = 1;
        } else {
            return StatusUtil.getFailedStatusWithMessage("类型参数错误");
        }

        User user = UserDao.dao.getUserByUsername(username);

        if (user == null) {
            return StatusUtil.getFailedStatusWithMessage("用户不存在");
        }

        if(oldPassword.equals(newPassword)){
            return StatusUtil.getFailedStatusWithMessage("新密码不能与原密码相同！");
        }

        if (type == 0) {
            if (!oldPassword.equals(user.getPassword())) {
                return StatusUtil.getFailedStatusWithMessage("原密码错误");
            }

            user.setPassword(newPassword);
        } else {
            if (!oldPassword.equals(user.getOperatePassword())) {
                return StatusUtil.getFailedStatusWithMessage("原密码错误");
            }

            user.setOperatePassword(newPassword);
        }

        Map<String, Object> res = new HashMap<>();
        Map<String, Object> data = new HashMap<>();
        data.put("newPassword", newPassword);
        res.put("data", data);

        return StatusUtil.getStatus(res, user.update(), "修改成功", "修改失败");
    }

    public Map<String, Object> updateUserInfo(String username) {
        if (Tools.checkParaIsInValidate(username)) {
            return StatusUtil.getFailedStatusWithMessage("参数无效！");
        }

        User user = UserDao.dao.getUserByUsername(username);

        if (user == null) {
            return StatusUtil.getFailedStatusWithMessage("用户不存在");
        }

        Company company = CompanyDao.dao.getCompanyByCompanyId(user.getCompanyId());

        Map<String, Object> res = new HashMap<>();
        Map<String, Object> data = new HashMap<>();
        data.put("user", user);
        if (company != null) {
            data.put("company", company);
            List<Product> products = ProductDao.dao.getProductsByCompanyId(company.getCompanyId());
            if (products.size() != 0) {
                data.put("products", products);
            }
            List<Formula> formulas = FormulaDao.dao.getFormulasByCompanyId(company.getCompanyId());
            if (formulas.size() != 0) {
                data.put("formulas", formulas);
            }
            List<UserRelation> relations = UserRelationDao.dao.getUserRelationsBySourceUsername(username);
            if (relations.size() != 0) {
                data.put("relations", relations);
            }
        }
        res.put("data", data);

        return StatusUtil.getSuccessStatusWithMessage(res, "更新成功");
    }

    public Map<String, Object> updateUser(
            String username,
            String nickName,
            String position,
            String tel,
            String mobile,
            String QQ,
            String wechat) {

        if (Tools.checkParaIsInValidate(username)
                || Tools.checkParaIsNullPoint(nickName, position, tel, mobile, QQ, wechat)) {
            return StatusUtil.getFailedStatusWithMessage("参数无效！");
        }

        User user = UserDao.dao.getUserByUsername(username);

        if (user == null) {
            return StatusUtil.getFailedStatusWithMessage("用户不存在！");
        }

        user.setMobile(mobile);
        user.setQQ(QQ);
        user.setWechat(wechat);
        user.setNickName(nickName);
        user.setPosition(position);
        user.setTel(tel);

        //更新当前用户已经建立的用户关系
        List<UserRelation> relations = UserRelationDao.dao.getUserRelationsByTargetUsername(username);
        for (UserRelation relation : relations) {
            relation.setContact(nickName);
            relation.setContactPinyin(Tools.toHanyuPinyin(nickName));
            relation.setTel(tel);
            if (!relation.update()) {
                return StatusUtil.getFailedStatusWithMessage("保存失败时更新用户关系失败！");
            }
        }

        return StatusUtil.getStatus(user.update(), "保存成功！", "保存失败");
    }

    public Map<String,Object> findPassword(String username, String password){
        if(Tools.checkParaIsInValidate(username,password)){
            return StatusUtil.getFailedStatusWithInvalidParaMessage();
        }

        User user = UserDao.dao.getUserByUsername(username);

        if(user==null){
            return StatusUtil.getFailedStatusWithMessage("用户不存在！");
        }

        user.setPassword(password);

        if(!user.update()){
            return StatusUtil.getFailedStatusWithMessage("重置密码失败！");
        }

        return StatusUtil.getSuccessStatusWithMessage("重置密码成功！");
    }
}
